package crmdna.inventory;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.gson.Gson;
import com.googlecode.objectify.ObjectifyFilter;

import crmdna.client.Client;
import crmdna.common.UnitUtils.PhysicalQuantity;
import crmdna.common.UnitUtils.ReportingUnit;
import crmdna.group.Group;
import crmdna.group.Group.GroupProp;
import crmdna.user.User;
import crmdna.user.UserCore;

public class InventoryItemTest {
	private final LocalServiceTestHelper datastoreHelper = new LocalServiceTestHelper(
			new LocalDatastoreServiceTestConfig().setApplyAllHighRepJobPolicy());

	private final String client = "isha";
	private final String validUser = "valid@login.com";
	private GroupProp sgp;
	private GroupProp kl;
	InventoryItemTypeProp book;

	@Before
	public void setUp() {
		datastoreHelper.setUp();
		ObjectifyFilter.complete();

		Client.create(client);
		sgp = Group.create(client, "Singapore", User.SUPER_USER);
		assertEquals(1, sgp.groupId);

		kl = Group.create(client, "KL", User.SUPER_USER);
		assertEquals(2, kl.groupId);

		User.create(client, validUser, sgp.groupId, User.SUPER_USER);
		assertEquals(1, UserCore.get(client, validUser).toProp().userId);

		book = InventoryItemType.create(client, "Book", User.SUPER_USER);
		assertEquals(1, book.inventoryItemTypeId);
	}

	@After
	public void tearDown() {
		ObjectifyFilter.complete();
		datastoreHelper.tearDown();
	}

	@Test
	public void queryTest() {
		InventoryItemProp forestFlower = InventoryItem.create(client,
				sgp.groupId, book.inventoryItemTypeId, "Forest Flower",
				PhysicalQuantity.NUMBER, ReportingUnit.NUMBER, 0,
				User.SUPER_USER);
		
		assertEquals(1, forestFlower.inventoryItemId);

		InventoryItemProp mysticEye = InventoryItem
				.create(client, sgp.groupId, book.inventoryItemTypeId,
						"Mystic Eye", PhysicalQuantity.NUMBER, ReportingUnit.NUMBER, 3, User.SUPER_USER);
		assertEquals(2, mysticEye.inventoryItemId);

		InventoryItemProp midnights = InventoryItem.create(client, kl.groupId,
				book.inventoryItemTypeId, "Midnights with Mystic",
				PhysicalQuantity.NUMBER, ReportingUnit.NUMBER, 4, User.SUPER_USER);
		assertEquals(3, midnights.inventoryItemId);

		InventoryItemQueryCondition qc = new InventoryItemQueryCondition();
		qc.groupId = (long) 1;
		List<InventoryItemProp> props = InventoryItem.query(client, qc);

		assertEquals(2, props.size());
		System.out.println("props: " + new Gson().toJson(props));
	}
}
